﻿#region License, Terms and Conditions
//
// Copyright (c) 2008, aspZone.com
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the <organization> nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY aspZone.com ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Author(s):
//  John Rudolf Lewis (http://aspZone.com)
//
#endregion

using System.Collections.Generic;
using System.Linq;
using Iesi.Collections.Generic;

namespace aspZone.Commerce.Model
{
    public class Product
    {
        public Product()
        {
            this.Images = new List<Image>();
            this.Categories = new HashedSet<Category>();
            this.Active = true;
        }

        /// <summary>
        /// Gets the primary id of the product
        /// </summary>
        public virtual int Id { get; set; }

        /// <summary>
        /// Gets or sets the unique name of the Product. To be used in URLs and Title tags.
        /// </summary>
        public virtual string Name { get; set; }

        public virtual string UrlName
        {
            get
            {
                return this.Name.RemoveAllNonAlphaNumeric();
            }
        }

        /// <summary>
        /// Gets or sets the description of the Product.
        /// </summary>
        /// <remarks>
        /// To be displayed in a product detail view. Rich HTML content allowed.
        /// </remarks>
        public virtual string Description { get; set; }

        /// <summary>
        /// Gets or sets the price of the Product;
        /// </summary>
        public virtual decimal Price { get; set; }

        /// <summary>
        /// Gets or sets the discount price of the Product
        /// </summary>
        public virtual decimal DiscountAmount { get; set; }

        /// <summary>
        /// Gets or sets the handling charge for the product
        /// </summary>
        public virtual decimal HandlingCharge { get; set; }

        /// <summary>
        /// Gets or sets the weight for the product.
        /// </summary>
        public virtual decimal Weight { get; set; }

        /// <summary>
        /// Gets or sets how many of this product are available for purchase.
        /// </summary>
        public virtual int QuantityAvailable { get; set; }

        /// <summary>
        /// Gets the list of Images for this product
        /// </summary>
        public virtual IList<Image> Images { get; private set; }

        /// <summary>
        /// Gets the list of Categories for this product
        /// </summary>
        public virtual ISet<Category> Categories { get; private set; }

        /// <summary>
        /// Gets or sets a value indicating whether this product is active
        /// </summary>
        public virtual bool Active { get; set; }

        /// <summary>
        /// Gets the default image for this product.
        /// </summary>
        public virtual Image DefaultImage 
        {
            get
            {
                if (this.Images.Count > 0)
                {
                    return this.Images[0];
                }
                else
                {
                    return new Image ("NoImageFound_full.jpg", "NoImageFound_thumb.jpg");
                }
            }
        }

        public virtual Category DefaultCategory
        {
            get
            {
                return this.Categories.FirstOrDefault();
            }
        }


        public override bool Equals(object obj)
        {
            if (obj is Product)
            {
                Product compareTo = (Product)obj;
                if (compareTo.Id == 0 || this.Id == 0)
                {
                    return compareTo.Name == this.Name;
                }
                else
                {
                    return compareTo.Id == this.Id;
                }
            }
            else
            {
                return base.Equals(obj);
            }

        }

        public override int GetHashCode()
        {
            if (this.Id > 0)
            {
                return this.Id.GetHashCode();
            }
            else
            {
                return base.GetHashCode();
            }
        }
    }
}
